<template>
  <!--     methods: {
    changeFile (e) {
      const file = e.target.files[0]
      console.log(e.target.files[0])
      cos.putObject(
        {
          Bucket: 'dimi-1312957979' /* 必须 */,
          Region: 'ap-hongkong' /* 存储桶所在地域，必须字段 */,
          Key: file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: file, // 上传文件对象
          onProgress: function (progressData) {
            console.log(JSON.stringify(progressData))
          }
        },
        function (err, data) {
          console.log(err || data)
        }
      )
    }
  }
}
 -->
  <div class="upload-img">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="value" v-errorImg :src="value" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <div v-if="value" class="del el-icon-circle-close" @click="del" />
    <div v-if="value" class="show el-icon-zoom-in" @click="showBig = true" />
    <transition name="progress">
      <el-progress
        v-if="show"
        class="progress"
        type="circle"
        :percentage="percentage"
        :width="178"
      />
    </transition>
    <el-dialog title="预览" :visible.sync="showBig">
      <div style="text-align:center">
        <img class="img-big" :src="value" alt="" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
let cos = new COS({
  SecretId: 'AKIDmLehhdJkqjLglbMtrlOlwHMAg7MNHyYu', // 身份识别ID
  SecretKey: '92dx1oLbfT2KK3XkiRvA7nDOM5ZmlTEr' // 身份密钥
})
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      //   imageUrl: ''
      showBig: false,
      show: false,
      percentage: 0
    }
  },
  methods: {
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    httpRequest (res) {
      console.log(res.file)
      this.show = true
      cos.putObject(
        {
          Bucket: 'dimi-1312957979' /* 必须 */,
          Region: 'ap-hongkong' /* 存储桶所在地域，必须字段 */,
          Key: res.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          onProgress: progressData => {
            console.log(JSON.stringify(progressData))
            this.percentage = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.show = false
              }, 1000)
            }
          }
        },
        (err, data) => {
          console.log(err || data)
          this.$emit('input', 'http://' + data.Location)
          //   this.imageUrl = 'http://' + data.Location
        }
      )
    },
    del () {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.upload-img {
  position: relative;
  width: 178px;
  &:hover {
    .del {
      display: block;
    }
    .show {
      display: block;
    }
  }
  .del,
  .show {
    position: absolute;
    top: 0;
    right: 0;
    font-size: 30px;
    color: #f00;
    cursor: pointer;
    display: none;
    &:hover {
      color: goldenrod;
    }
  }
  .show {
    right: 50px;
  }
  .img-big {
    width: 80%;
  }
  .progress {
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.4);
    ::v-deep .el-progress_text {
      color: #fff;
    }
  }
  .progress-emter,
  .progress-leave-to {
    opacity: 0;
  }
  .progress-enter-active,
  .progress-leave-active {
    transition: opacity 0.5s;
  }
}
</style>
